package com.jbp.utils;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;

import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author fei.hu
 * @since 2017/05/26
 * @version R0.0.100
 * @category 字符串转换成秒数,从1970/01/01 到当前的秒数
 */
public class StrToMillsUtil {

	public static String dateToStr(Date date) {
		return DateFormatUtils.format(date,"yyyy-MM-dd HH:mm:ss");
	}
	/**
	 * 时间 转换成秒数
	 * @param date 
	 *            转换字符串 ,如果date为空,返回0,格式:yyyy-MM-dd
	 * @return
	 */
	public static int dateToMillsShort( Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return strToMillsShort(sdf.format(date));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 时间 转换成秒数 1
	 * @param date 
	 *            转换字符串 ,如果date为空,返回0,格式:yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static int dateToMillsLong( Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return strToMillsLong(sdf.format(date));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 字符串转换成秒数
	 * 
	 * @param date
	 *            转换字符串 ,如果date为空,返回0,格式:yyyy-MM-dd
	 * @return
	 */
	public static int strToMillsShort( String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			if (StringUtils.isNotBlank(date)) {
				return (int) (sdf.parse(date).getTime() / 1000);
			} else {
				return 0;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 字符串转换成秒数
	 * 
	 * @param date
	 *            转换字符串 ,如果date为空,返回0,格式:yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static int strToMillsLong( String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		try {
			if (StringUtils.isNotBlank(date)) {
				return (int) (sdf.parse(date).getTime() / 1000);
			} else {
				return 0;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 秒数转字符串  yyyy-MM-dd hh:mm:ss
	 * @param mills
	 * @return
	 */
	public static String millsToStrLong(long mills) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			if (mills != 0) {
				Date date = new Date(mills);
				String dateStr = sdf.format(date);
				return dateStr;
			} else {
				return "0";
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "0";
		}
	}
	
	/**
	 * Date转字符串  yyyy-MM-dd hh:mm:ss
	 * @param
	 * @return
	 */
	public static String millsToStrDate(Date date, String format) {
		SimpleDateFormat sdf = null;
		if(StringUtils.isNotEmpty( format )){
			sdf = new SimpleDateFormat(format);
		}else{
			sdf = new SimpleDateFormat("yyyy-MM-dd");
		}
		
		try {
			if (date != null) {
//				Date date = new Date((long) mills);
				String dateStr = sdf.format(date);
				return dateStr;
			} else {
				return "0";
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "0";
		}
	}
	
	
	/**
	 * 秒数转字符串 yyyy-MM-dd
	 * @param mills 
	 * @return
	 */
	public static String millsToStrShort(int mills) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if (mills != 0) {
				Date date = new Date((long) mills);
				String dateStr = sdf.format(date);
				return dateStr;
			} else {
				return "0";
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "0";
		}
	}
	
	/**
	 * 自有加密算法，请注意保密
	 * @author yideng.
	 * @param password 原始明文密码
	 * @return 加密过后的密码
	**/
	public static String passwordEncode(String password , String salt){
			String resultPassword = null,tempStr = "";
			if (password != null){
				try{
					MessageDigest md5 = MessageDigest.getInstance("MD5");
					java.util.Base64.Encoder base64 = java.util.Base64.getEncoder();
					tempStr = forEachs(password, 0x55).concat(salt).concat(forEachs(password, 0x55));
					resultPassword = new String(base64.encode(md5.digest(tempStr.getBytes("utf-8"))),"utf-8");
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			return resultPassword;
		}

	/**
		 * 将一个字符串循环N次
		 * @param nativeStr 字符串 
		 * @param count 循环次数
		 * @return 返回新串
		 */
	private static String forEachs(String nativeStr, int count){
			StringBuilder sb = new StringBuilder();
				for(int i=0;i<count;i++)
					sb.append(nativeStr);
			return sb.toString();
	}
	

}
